package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.service.AdminService;
import com.atguigu.service.PermissionService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller

public class FrameController {
    @Reference
    private PermissionService permissionService;
    @Reference
    private AdminService adminService;

    @RequestMapping
    public String toIndex(Model model){
        //通过Security的上下文里获取登录对象信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User)authentication.getPrincipal();
        /*//这里还没有实现用户登录传进的数据,我们得手动定义一个用户
        //Long adminId=1L;
        Long adminId=10L;
        Admin admin = adminService.getById(adminId);*/
        Admin admin = adminService.getByUsername(user.getUsername());
        //这里就跳到后台管理的首页,我们得在这里获得用户菜单权限
        List<Permission> permissionList = permissionService.findMenuPermissionByAdminId(admin.getId());
        //把数据传到共享域给前端
        model.addAttribute("permissionList",permissionList);
        //index页面需要用户得头像昵称,所以也得把用户信息传过去.
        model.addAttribute("admin",admin);
        return "frame/index";
    }

    @RequestMapping("/main")
    public String main(){

        return "frame/main";
    }


    @RequestMapping("/loginPage")
    public String loginPage(){
        return "frame/login";
    }

    //无权限时的友好提示页面跳转
    @GetMapping("/auth")
    public String auth() {
        return "frame/auth";
    }
}
